<?php
//No direct access
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.view');

class OwnserviceViewTransaction extends JView
{
	function display($tpl = null) {

		global $mainframe, $option;

		// Initialise variables
		$user			= & JFactory::getUser();
		$db				= & JFactory::getDBO();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 		= & $mainframe->getParams('com_ownservice');
		$level			= & $ownuser->get('level_id');
		$pathway  	= & $mainframe->getPathway();
		
		//Set page title information
		$menus		= &JSite::getMenu();
		$menu		= $menus->getActive();

		// Set vars
		$level		= ($user->gid < 25)?$level:6;
		$nullDate 	= $db->getNullDate();

		if( $this->getLayout() == 'form' ) {
			$this->_displayForm($tpl);
			return;
		}

		if( $this->getLayout() == 'customerform' ) {
			$this->_displayCustomerForm($tpl);
			return;
		}

		if( $this->getLayout() == 'print' ) {
			$this->_displayPrint($tpl);
			return;
		}

		if( $this->getLayout() == 'receipt' ) {
			$this->_displayPrint($tpl);
			return;
		}
		
		if( $this->getLayout() == 'detailcancelled' ) {
			$this->_displaydetailcancelled($tpl);
			return;
		}

		if($user->gid < 25) {
			$canView		= Ownservice::checkAccess('transaction', 'view', 'transaction', 'all');
			if ( !$canView) {
				//JError::raiseError( 403, JText::_("ALERTNOTAUTH") );
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}
		
		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$row		= $this->get('Data');
		
		// Create a user access object for the current user
		$access = new stdClass();
		$access->canEdit				 = ($user->gid < 25)? Ownservice::checkAccess('transaction','edit','transaction','all'):1;
		$access->canApprovalTransferred	 = ($user->gid < 25)? Ownservice::checkAccess('transaction','approvaltransferred','transaction','all'):1;


		// because the application sets a default page title, we need to get it
		// right from the menu item itself
		if (is_object( $menu )) {
			$menu_params = new JParameter( $menu->params );
			if (!$menu_params->get( 'page_title')) {
				$params->set('page_title',	JText::_('OWNSERVICE_TRANSACTION_DETAIL'));
			}
		} else {
			$params->set('page_title',	JText::_('OWNSERVICE_TRANSACTION_DETAIL'));
		}
		$document->setTitle( $params->get( 'page_title' ) );
		
		
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('level', $level);
		$this->assignRef('access', $access);
		$this->assignRef('nullDate', $nullDate);
		$this->assignRef('params', $params);

		parent::display($tpl);
	}

	function _displayForm($tpl) {

		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$db			= & JFactory::getDBO();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');
		$pathway  	= & $mainframe->getPathway();
		
		//Set page title information
		$menus		= &JSite::getMenu();
		$menu		= $menus->getActive();

		//get vars
		$gid	= $user->gid;
		$level	= & $ownuser->get('level_id');
		$level	= ($gid < 25) ? $level : 6;

		$nullDate = $db->getNullDate();
		
		JHTML::_('behavior.mootools');
		JHTML::_('behavior.calendar');

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addScript(JURI::base().'/components/com_ownservice/assets/js/ownservice.js');
		
		//add js autocomplete
		$document->addScript('components/com_ownservice/assets/js/jquery-1.7.2.min.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.bgiframe.min.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.ajaxQueue.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-autocomplete/jquery.autocomplete.min.js');
		//add css autocomplete
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-autocomplete/jquery.autocomplete.css');
		
		//ui
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-ui-1.9.1/themes/base/jquery.ui.all.css');
		//$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/jquery-1.8.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/external/jquery.bgiframe-2.1.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.core.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.widget.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.mouse.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.draggable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.position.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.resizable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.dialog.js');
	
		$js = "
				function selectUser(cid, membername)
				{
					document.adminForm.membername.value=membername;
					document.adminForm.user_id.value=cid;
				
					checkexpire(cid);
					getImage(cid);
					getAccount(cid);
					getCurrency(cid);
					getLastSenderTransfer(cid);
					document.getElementById('sbox-window').close();
				}
				
				function checkMaxLastSenderTransfer(cid, day){
					var amount = jQuery('#amount').val();
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=ajaxcheckmaxlastsendertransfer&".JUtility::getToken()."=1\", {id:cid, value:amount, days:day},
					function(data){
						if(data.over == 1){
							getMaxLastSenderTransfer(cid, data.overday);
							jQuery(\"#maxsendertransfer\").dialog({ 
								height: 'auto', 
								width: '500',
								title: \"Notifications\",
								position: { my: \"center\", at: \"center\", of: window },
								dialogClass: \"no-close\",
									buttons: [
										{
											text: \"OK\",
											click: function() {
												jQuery( this ).dialog( \"close\" );
											}
										}
									]
							});
						}
					}
					,'json'
					);
				}

				function getMaxLastSenderTransfer(cid, day){
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=ajaxgetmaxlastsendertransfer&".JUtility::getToken()."=1\", { id:cid, days:day},
						function(data){
							jQuery( \"#maxsendertransfer\" ).html( data );
						}
					);
				}

				function getLastSenderTransfer(cid){
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=ajaxgetlastsendertransfer&".JUtility::getToken()."=1\", { id:cid},
						function(data){
							jQuery( \"#lastsendertransfer\" ).html( data );
						}
					);
				}

				function checkMaxLastReceiverTransfer(cid, ac, day){
					var amount = jQuery('#amount').val();
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=ajaxcheckmaxlastreceivertransfer&".JUtility::getToken()."=1\", { account:ac, id:cid, value:amount, days:day},
					function(data){
						if(data.over == 1){
							getMaxLastReceiverTransfer(ac, data.overday);
							jQuery(\"#maxreceivertransfer\").dialog({ 
								height: 'auto', 
								width: '500',
								title: \"Notifications\",
								position: { my: \"center\", at: \"center\", of: window },
								dialogClass: \"no-close\",
									buttons: [
										{
											text: \"OK\",
											click: function() {
												jQuery( this ).dialog( \"close\" );
											}
										}
									]
							});
						}
					}
					,'json'
					);
				}

				function getMaxLastReceiverTransfer(ac, day)
				{
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=ajaxgetlastreceivertransfer&".JUtility::getToken()."=1\", { account:ac, days:day},
						function(data){
							jQuery( \"#maxreceivertransfer\" ).html( data );
						}
					);
				}

				function checkexpire(cid){
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=checkexpire&".JUtility::getToken()."=1\", { id:cid},
					function(data){
						if(data.fileid == 0) {
							showdialog(data, 'Please update ID!', true);
						} else if(data.days >0 && data.days <= 30){
							showdialog(data, 'ID expiring soon '+data.expiredate, false);
						} else if(data.days <= 0) {
							showdialog(data, 'ID Expired. Unable to make a transaction '+data.expiredate, true);	
						}
					}
					,'json'
					);
				}
				
				function showdialog(data, msg, status) {
					jQuery(\"#msg\").text(msg);
					jQuery(\"#memberid\").val(data.id);
					jQuery(\"#dialog\").dialog();
				}
	
				jQuery(document).ready(function () {
					jQuery(\"#membername\").autocomplete(\"index.php?option=com_ownservice&controller=transaction&task=viewmembers&format=raw&".JUtility::getToken()."=1\", {
					width: 260,
					max: 100,
					matchContains: false,
					mustMatch: false,
					selectFirst: false,
					dataType: \"json\",
					parse: function(data) {
						return jQuery.map(data, function(row) {
							return {
								data: row,
								value: row.name,
								result: row.name
							};
						});
					},
					formatItem: function(row) {
						return row.name;
					}
					}).result(function(e, row) {
						getImage(row.id);	
						getAccount(row.id);
						getCurrency(row.id);
						getLastSenderTransfer(row.id);
						checkexpire(row.id);
						jQuery(\"#user_id\").val(row.id);
					})
					
					var uid = jQuery('#user_id').val();
					jQuery(\"#rate_id\").change(function() {
						getExchange(uid);
					});
				});
				
				function getImage(uid)
				{
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=getImage&".JUtility::getToken()."=1\", { id:uid},
					function(data){
						jQuery('#mphoto').html(data);
						window.addEvent('domready', function() {
							SqueezeBox.initialize();
							$$('a.img-preview').each(function(el) {
								el.addEvent('click', function(e) {
									new Event(e).stop();
									SqueezeBox.fromElement(el);
								});
							});
						});
					});
				}
				
				function getAccount(uid)
				{
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=getAccounts&".JUtility::getToken()."=1\", { id:uid},
					function(data){
						jQuery('#accountno').html(data);
					});
				}

				function getCurrency(uid)
				{
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=getCurrency&".JUtility::getToken()."=1\", { id:uid},
					function(data){
						jQuery('#currency').html(data);
						getExchange(uid);
						jQuery(\"#rate_id\").change(function() {
							getExchange(uid);
						});
					});
				}
				
				function getExchange(uid)
				{
					var rateid	= jQuery('#rate_id').val();
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=getExchange&".JUtility::getToken()."=1\", { id:uid,rate_id:rateid},
					function(data){
						jQuery('#rate').val(data.rate);
						jQuery('.code').text(data.code);
					}
					,'json'
					);
				}
				
				function getTotalExchange()
				{
					var f = document.adminForm;
					
					if(!isSelectSender()){
						return false;
					}
					
					f.task.value = 'getTotal';
					
					$('adminForm').send({
						onComplete: function(response) {
							checkMaxLastSenderTransfer(f.user_id.value, '10');
							checkMaxLastReceiverTransfer(f.user_id.value, f.account.value, '10');
							var resp = Json.evaluate( response );
							$('fee').value=resp.fee;
							$('bankfee').value=resp.bankfee;
							$('total').value=parseFloat(resp.total).toFixed(2);
							
							if(f.amount.value >= 10000){
								showOtherDetail(f.user_id.value, f.account.value);
							} else {
								hiddenOtherDetail();
							}
						}
					});
				}
				
				function isSelectSender()
				{
					var f = document.adminForm;

					if(f.user_id.value <= 0) {
						alert('".JText::_('OWNSERVICE_SELECT_SENDER')."');
					   return false;
					}
					
					return true;
				}
				
				function getTotalTransfer() 
				{
					var f = document.adminForm;
					f.task.value = 'getTransfer';
					$('adminForm').send({
						onComplete: function(response) {
							checkMaxLastSenderTransfer(f.user_id.value, '10');
							checkMaxLastReceiverTransfer(f.user_id.value, f.account.value, '10');
							var resp = Json.evaluate( response );
							$('fee').value=resp.fee;
							$('bankfee').value=resp.bankfee;
							$('amount').value=parseFloat(resp.amount).toFixed(2);
							
							if(resp.amount >= 10000){
								showOtherDetail(f.user_id.value, f.account.value);
							} else {
								hiddenOtherDetail();
							}
						}
					});
				}
				
				window.addEvent('domready', function() {
					SqueezeBox.initialize();
					$$('a.img-preview').each(function(el) {
						el.addEvent('click', function(e) {
							new Event(e).stop();
							SqueezeBox.fromElement(el);
						});
					});
					
					$(\"reason\").addEvent(\"change\", function(event){
						var value = this.value;
						if(value == \"other\") {
							$(\"reasonother\").setProperty(\"style\", \"display:block\");
						} else {
							$(\"reasonother\").setProperty(\"style\", \"display:none\");
						}
					});
				});
				
				function submitbutton(pressbutton)
				{
					var form = document.adminForm;
					
					if (pressbutton == 'cancel') {
						submitform( pressbutton );
						return;
					}
					
					if (form.user_id.value == ''){
						alert('".JText::_( 'OWNSERVICE_PLEASE_SELECT_SENDER', true )."');
					} else if (form.account.value == ''){
						alert('".JText::_( 'OWNSERVICE_PLEASE_SELECT_ACCOUNT', true )."');
					} else if (form.amount.value == ''){
						alert('".JText::_( 'OWNSERVICE_PLEASE_INPUT_AMOUNT_TRANSFER', true )."');
					} else if (form.rate.value == ''){
						alert('".JText::_( 'OWNSERVICE_PLEASE_INPUT_EXCHANGE_RATE', true )."');
					}  else if (typeof form.occupation != 'undefined' && form.occupation.value == '') {
						alert('".JText::_('OWNSERVICE_PLEASE_INPUT_OCCUPATION', true )."');
						form.occupation.focus(); 
					}  else if(typeof form.birthday != 'undefined' && form.birthday.value == '') {
						alert('".JText::_('OWNSERVICE_PLEASE_INPUT_BIRTHDAY_RECEIVER', true )."');
						form.birthday.focus();
					} else {
						submitform( pressbutton );
					}
					
				}
				
				function showOtherDetail(uid, acc)
				{
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=getotherdetail&".JUtility::getToken()."=1\", { id:uid, account:acc},
					function(data){
						jQuery('#otherdetail').html(data);
						window.addEvent('domready', function() {Calendar.setup({
							inputField     :    \"birthday\",     // id of the input field
							ifFormat       :    \"%Y-%m-%d\",      // format of the input field
							button         :    \"birthday_img\",  // trigger for the calendar (button ID)
							align          :    \"Tl\",           // alignment (defaults to \"Bl\")
							singleClick    :    true
						});});
					});
				}

				function hiddenOtherDetail()
				{
					jQuery('#otherdetail').html('');
				}
			";
		$document->addScriptDeclaration($js);
			
		// Get data form the model
		$model		= $this->getModel();
		$row		= &$this->get('Data');
		$users		= &$this->get('Users');
		$accounts	= $model->getAccounts($row->user_id);
		$currencies = $model->getCurrencies($row->branch_id);
		$image		= $model->getImage($row->user_id);
		
		// Create a user access object for the current user
		$access = new stdClass();
		
		// check if it's an edit action
		if($row->id) {
			$canEdit			= ($gid<25)?Ownservice::checkAccess('transaction', 'edit', 'transaction', 'all'):1;
			$access->canEdit 	= $canEdit;
			if (!$canEdit) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		} else {
			$canAdd				= ($gid<25)?Ownservice::checkAccess('transaction', 'add', 'transaction', 'all'):1;
			$access->canAdd		= $canAdd;
			if (!$canAdd) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}
		
		
		$lists = array();

		//build receiver account select list
		$lists['account']	= OwnserviceHelper::buildbanksselect($accounts, 'account',$row->account,true, 'class="inputbox required" style="width:250px;"');

		//build reason select list
		$lists['reason']	= OwnserviceHelper::buildreasonsselect('reason', $row->reason, true, 'class="inputbox required" style="width:250px;"');
		
		//build status select list
		$status = array();
		$status[] = JHTML::_( 'select.option', '1',		JText::_('OWNSERVICE_PAID'));
		$status[] = JHTML::_( 'select.option', '2',		JText::_('OWNSERVICE_APPROVAL_PENDING'));
		$status[] = JHTML::_( 'select.option', '3',		JText::_('OWNSERVICE_TRANSFERRING'));
		$status[] = JHTML::_( 'select.option', '4',		JText::_('OWNSERVICE_SUCCESSFUL'));
		$status[] = JHTML::_( 'select.option', '5',		JText::_('OWNSERVICE_FAIL'));
		$status[] = JHTML::_( 'select.option', '6',		JText::_('OWNSERVICE_CORRECTED'));
		$status[] = JHTML::_( 'select.option', '7',		JText::_('OWNSERVICE_APPROVAL_PENDING_CORRECTED'));
		$status[] = JHTML::_( 'select.option', '8',		JText::_('OWNSERVICE_CANCELLED'));  
		
		//buils state select list
		$lists['status']	= JHTML::_('select.genericlist', $status, 'state', 'size="1" class="inputbox required" style="width:250px;"', 'value', 'text', $row->state );
		
		
		//build currency select list 
		$lists['currency']	= OwnserviceHelper::buildcurrenciesselect($currencies, 'rate_id',$row->rate_id,true, 'class="inputbox required" style="width:250px;"');
		
		// because the application sets a default page title, we need to get it
		// right from the menu item itself
		if (is_object( $menu )) {
			$menu_params = new JParameter( $menu->params );
			if (!$menu_params->get( 'page_title')) {
				if($row->id){
					$params->set('page_title', JText::_('OWNSERVICE_EDIT_TRANSFER'));
				} else {
					$params->set('page_title', JText::_('OWNSERVICE_NEW_TRANSFER'));
				}
			}
		} else {
			if($row->id){
				$params->set('page_title', JText::_('OWNSERVICE_EDIT_TRANSFER'));
			} else {
				$params->set('page_title', JText::_('OWNSERVICE_NEW_TRANSFER'));
			}
		}
		$document->setTitle( $params->get( 'page_title' ) );
		
		//resize image
		$path	= OwnserviceHelper::getParams('path', 'images/stories/ownservices');
		$info 	= @getimagesize(JPATH_SITE.DS.$path.DS.$image->userid.DS.$image->filename);
		if (($info[0] > 200) || ($info[1] > 200)) {
			$dimensions		= OwnserviceHelper::imageResize($info[0], $info[1], 200);
			$image->width	= $dimensions[0];
			$image->height	= $dimensions[1];
		} else {
			$image->width	= $info[0];
			$image->width	= $info[1];
		}
		
		JHTML::_('behavior.modal', 'a.modal');
		
		//assign data to document
		$this->assignRef('row', $row);
		$this->assignRef('image', $image);
		$this->assignRef('lists', $lists);
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('level', $level);
		$this->assignRef('access', $access);
		$this->assignRef('params', $params);
		$this->assignRef('nullDate', $nullDate);
		
		parent::display($tpl);
	}

	function _displayPrint() {

		global $mainframe, $option;

		// Initialise variables
		$user		= & JFactory::getUser();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();

		if($user->gid < 25) {
			$canView		= Ownservice::checkAccess('transaction', 'view', 'transaction', 'all');
			if (!$canView) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect( 'index.php?option=com_ownservice');
			}
		}
		
		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$row		= $this->get('Data');
		$level		= &$ownuser->get('level_id');

		// Create a user access object for the current user
		$access = new stdClass();
		$access->canEdit				 = ($user->gid < 25)? Ownservice::checkAccess('transaction','edit','transaction','all'):1;
		$access->canApprovalTransferred	 = ($user->gid < 25)? Ownservice::checkAccess('transaction','approvaltransferred','transaction','all'):1;

		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('level', $level);
		$this->assignRef('access', $access);

		parent::display($tpl);
	}

	function _displaydetailcancelled($tpl)
	{
		global $mainframe;
		
		// Initialise variables
		$user		= & JFactory::getUser();
		$db			= & JFactory::getDBO();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');
		$level		= & $ownuser->get('level_id');
		$pathway  	= & $mainframe->getPathway();
		
		//Set page title information
		$menus		= &JSite::getMenu();
		$menu		= $menus->getActive();

		// Set vars
		$level		= ($user->gid < 25)?$level:6;
		$nullDate 	= $db->getNullDate();
		
		if($user->gid < 25) {
			$canView	= Ownservice::checkAccess('transaction', 'view', 'transaction', 'all');
			if (!$canView) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}
		
		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		//add java script to document
		$document->addScript(JURI::base().'components/com_ownservice/assets/js/ownservice.js');

		//Get data form the model
		$model		= $this->getModel();
		$row		= $this->get('Data');
		
		// because the application sets a default page title, we need to get it
		// right from the menu item itself
		if (is_object( $menu )) {
			$menu_params = new JParameter( $menu->params );
			if (!$menu_params->get( 'page_title')) {
				$params->set('page_title',	JText::_('OWNSERVICE_TRANSACTION_DETAIL'));
			}
		} else {
			$params->set('page_title',	JText::_('OWNSERVICE_TRANSACTION_DETAIL'));
		}
		$document->setTitle( $params->get( 'page_title' ) );
		
		$this->assignRef('user', $user);
		$this->assignRef('ownuser', $ownuser);
		$this->assignRef('row', $row);
		$this->assignRef('level', $level);
		$this->assignRef('nullDate', $nullDate);
		$this->assignRef('params', $params);
		
		parent::display($tpl);
	}

	/**
	 *Customer transfer form
	*/
	function _displayCustomerForm($tpl)
	{
		global $mainframe;

		// Initialise variables
		$user		= & JFactory::getUser();
		$db			= & JFactory::getDBO();
		$ownuser	= & Ownservice::getUser();
		$document	= & JFactory::getDocument();
		$params 	= & $mainframe->getParams('com_ownservice');
		$pathway  	= & $mainframe->getPathway();
		
		//Set page title information
		$menus		= &JSite::getMenu();
		$menu		= $menus->getActive();

		//get vars
		$gid		= $user->gid;
		$level		= & $ownuser->get('level_id');

		if($gid < 25) {
			$canCustomerTransfers= Ownservice::checkAccess('transaction', 'customer', 'transaction', 'all');
			if ( !$canCustomerTransfers ) {
				JError::raiseWarning(500, JText::_('ALERTNOTAUTH') );
				$mainframe->redirect(JRoute::_('index.php?option=com_ownservice'));
			}
		}

		//add style to document
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/style.css');
		$document->addStyleSheet(JURI::base().'/components/com_ownservice/assets/css/nav.css');
		$document->addScript(JURI::base().'/components/com_ownservice/assets/js/ownservice.js');

		//add js autocomplete
		$document->addScript('components/com_ownservice/assets/js/jquery-1.7.2.min.js');

		//ui
		$document->addStyleSheet('components/com_ownservice/assets/js/jquery-ui-1.9.1/themes/base/jquery.ui.all.css');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/external/jquery.bgiframe-2.1.2.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.core.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.widget.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.mouse.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.draggable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.position.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.resizable.js');
		$document->addScript('components/com_ownservice/assets/js/jquery-ui-1.9.1/ui/jquery.ui.dialog.js');

		$js = "
				function getTotalExchange()
				{
					var f = document.adminForm;
					
					if(!isSelectSender()){
						return false;
					}
					
					f.task.value = 'getTotal';
					
					$('adminForm').send({
						onComplete: function(response) {
							checkMaxLastSenderTransfer(f.user_id.value, '10');
							checkMaxLastReceiverTransfer(f.user_id.value, f.account.value, '10');
							var resp = Json.evaluate( response );
							$('fee').value=resp.fee;
							$('bankfee').value=resp.bankfee;
							$('total').value=parseFloat(resp.total).toFixed(2);
							
							if(f.amount.value >= 10000){
								showOtherDetail(f.user_id.value, f.account.value);
							} else {
								hiddenOtherDetail();
							}
						}
					});
				}
				function getTotalTransfer() 
				{
					var f = document.adminForm;
					f.task.value = 'getTransfer';
					$('adminForm').send({
						onComplete: function(response) {
							checkMaxLastSenderTransfer(f.user_id.value, '10');
							checkMaxLastReceiverTransfer(f.user_id.value, f.account.value, '10');
							var resp = Json.evaluate( response );
							$('fee').value=resp.fee;
							$('bankfee').value=resp.bankfee;
							$('amount').value=parseFloat(resp.amount).toFixed(2);
							
							if(resp.amount >= 10000){
								showOtherDetail(f.user_id.value, f.account.value);
							} else {
								hiddenOtherDetail();
							}
						}
					});
				}
				function isSelectSender()
				{
					var f = document.adminForm;

					if(f.user_id.value <= 0) {
						alert('".JText::_('OWNSERVICE_SELECT_SENDER')."');
					   return false;
					}
					
					return true;
				}
				function showOtherDetail(uid, acc)
				{
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=getotherdetail&".JUtility::getToken()."=1\", { id:uid, account:acc},
					function(data){
						jQuery('#otherdetail').html(data);
						window.addEvent('domready', function() {Calendar.setup({
							inputField     :    \"birthday\",     // id of the input field
							ifFormat       :    \"%Y-%m-%d\",      // format of the input field
							button         :    \"birthday_img\",  // trigger for the calendar (button ID)
							align          :    \"Tl\",           // alignment (defaults to \"Bl\")
							singleClick    :    true
						});});
					});
				}
				function hiddenOtherDetail()
				{
					jQuery('#otherdetail').html('');
				}
				function checkMaxLastSenderTransfer(cid, day){
					var amount = jQuery('#amount').val();
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=ajaxcheckmaxlastsendertransfer&".JUtility::getToken()."=1\", {id:cid, value:amount, days:day},
					function(data){
						if(data.over == 1){
							getMaxLastSenderTransfer(cid, day);
							jQuery(\"#maxsendertransfer\").dialog({ 
								height: 'auto', 
								width: '500',
								title: \"Sender Transferred\",
								position: { my: \"center\", at: \"center\", of: window },
								dialogClass: \"no-close\",
									buttons: [
										{
											text: \"OK\",
											click: function() {
												jQuery( this ).dialog( \"close\" );
											}
										}
									]
							});
						}
					}
					,'json'
					);
				}
				function getMaxLastSenderTransfer(cid, day){
					jQuery.post(\"index.php?option=com_ownservice&controller=transaction&task=ajaxgetmaxlastsendertransfer&".JUtility::getToken()."=1\", { id:cid, days:day},
						function(data){
							jQuery( \"#maxsendertransfer\" ).html( data );
						}
					);
				}
				window.addEvent('domready', function() {
					$(\"reason\").addEvent(\"change\", function(event){
						var value = this.value;
						if(value == \"other\") {
							$(\"reasonother\").setProperty(\"style\", \"display:block\");
						} else {
							$(\"reasonother\").setProperty(\"style\", \"display:none\");
						}
					});
				});
			";

		$document->addScriptDeclaration($js);

		// Get data form the model
		$model		= $this->getModel();
		$row		= &$this->get('CustomerTransfer');
		$accounts	= $model->getAccounts($row->user_id);
		$currencies = $model->getCurrencies($row->branch_id);
		$image		= $model->getImage($row->user_id);

		$lists = array();

		//build receiver account select list
		$lists['account']	= OwnserviceHelper::buildbanksselect($accounts, 'account',$row->account,true, 'class="inputbox required" style="width:250px;"');

		//build reason select list
		$lists['reason']	= OwnserviceHelper::buildreasonsselect('reason', $row->reason, true, 'class="inputbox required" style="width:250px;"');

		//build currency select list 
		$lists['currency']	= OwnserviceHelper::buildcurrenciesselect($currencies, 'rate_id',$row->rate_id,true, 'class="inputbox required" style="width:250px;"');
		
		//resize image
		$path	= OwnserviceHelper::getParams('path', 'images/stories/ownservices');
		$info 	= @getimagesize(JPATH_SITE.DS.$path.DS.$image->userid.DS.$image->filename);
		if (($info[0] > 200) || ($info[1] > 200)) {
			$dimensions		= OwnserviceHelper::imageResize($info[0], $info[1], 200);
			$image->width	= $dimensions[0];
			$image->height	= $dimensions[1];
		} else {
			$image->width	= $info[0];
			$image->width	= $info[1];
		}
		
		JHTML::_('behavior.modal', 'a.modal');

		$params->set( 'page_title', JText::_('OWNSERVICE_EDIT_CUSTOMER_TRANSFER') );
		//set title
		$document->setTitle( $params->get( 'page_title' ) );

		//assign data to document
		$this->assignRef('row', $row);
		$this->assignRef('image', $image);
		$this->assignRef('lists', $lists);
		$this->assignRef('params', $params);

		parent::display($tpl);	
	}
}